package com.chima.dao;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.Resource;
import com.chima.domain.Rose;

public class RoseDao extends HibernateDaoSupport {

	public List<Rose> findallRoses() {
		return this.getSession().createQuery(" from Rose").list();
	}
	public List<Rose> findByCriteria(DetachedCriteria criteria, int firstResult,int maxResults) {
		return this.getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults);
	}

	public int findByTotalCount() {
		List list= this.getHibernateTemplate().find("select count(*) from Rose");
		long temp = (Long) list.get(0);
		return (int) temp;
	}
//为角色附对应的资源
	public boolean saveorupdate(int id, int id2) {
		     Session session=this.getSession();
		    String sql2="insert into t_rose_resource (roseID,resourceID) VALUES(?,?)";
		    session.createSQLQuery(sql2).setInteger(0, id).setInteger(1, id2).executeUpdate();
		    super.releaseSession(session);
			return true;
	}
	public boolean save(Rose rose) {
		this.getHibernateTemplate().save(rose);
		return true;
	}
	public void  editRose(Rose rose) {
		this.getHibernateTemplate().update(rose);
		
	}
	public void deleteRose(Integer rid) {
		Session session=this.getSession();
		 String sql="delete from t_rose_resource where roseID=?";
		 session.createSQLQuery(sql).setInteger(0, rid).executeUpdate();
		this.getHibernateTemplate().delete(this.getHibernateTemplate().load(Rose.class, rid));	
		  super.releaseSession(session);
	}
	public void delRose_resource(int rId) {
		 Session session=this.getSession();
		 String sql1=" delete  from t_rose_resource where roseID=?";
		 session.createSQLQuery(sql1).setParameter(0, rId).executeUpdate();
		 super.releaseSession(session);
		 
	}
	public List<Resource> resourcesfindbyroseid(int id) {
		   Session session=this.getSession();
		    String sql="select * from t_resource where id in(select resourceID from t_rose_resource where roseID=?)";
		
			List<Resource> l = ((SQLQuery) session.createSQLQuery(sql).setParameter(0, id)).addEntity(Resource.class).list();
			super.releaseSession(session);
			return l;
		}

}
